﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Symbols;
using ESRI.ArcGIS.Client.Tasks;
using ESRI.ArcGIS.Client.Toolkit;

namespace ENMAX_ArcGIS
{
    public sealed class MapLayersManager
    {
        public delegate void MapLayerAddedDelegate(Layer aMapLayer);
        public delegate void MapLayerDeletedDelegate(Layer aMapLayer);

        public event MapLayerAddedDelegate MapLayerAdded;
        public event MapLayerDeletedDelegate MapLayerDeleted;

        public static MapLayersManager Instance { get; private set; }

        static MapLayersManager() { Instance = new MapLayersManager(); }

        /* Keep list of map layers */
        private LayerCollection layers;
        public LayerCollection MapLayers
        {
            get
            {
                return layers;
            }
        }

        /* Keep track of all MapFrames currently active */
        private List<MapFrame> mapFrames;

        MapLayersManager()
        {
            layers = new LayerCollection();

            InitializeDefaultMapLayers();
        }

        private void InitializeDefaultMapLayers()
        {
            ArcGISTiledMapServiceLayer topoLayer = new ArcGISTiledMapServiceLayer();
            topoLayer.ID = "ShadedRelief";
            topoLayer.Opacity = 1;
            topoLayer.Visible = true;
            topoLayer.Url = "http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_ShadedRelief_World_2D/MapServer";

            ArcGISTiledMapServiceLayer imageryLayer = new ArcGISTiledMapServiceLayer();
            imageryLayer.ID = "Imagery";
            imageryLayer.Opacity = 1;
            imageryLayer.Visible = true;
            imageryLayer.Url = "http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_Imagery_World_2D/MapServer";

            ArcGISTiledMapServiceLayer streetLayer = new ArcGISTiledMapServiceLayer();
            streetLayer.ID = "StreetMap";
            streetLayer.Opacity = 1;
            streetLayer.Url = "http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer";
           
            GraphicsLayer g1 = new GraphicsLayer();
            g1.ID = "TroubleTicketsLayer";
            GraphicsLayer g2 = new GraphicsLayer();
            g2.ID = "VehiclesLayer";
            GraphicsLayer g3 = new GraphicsLayer();
            g3.ID = "AnnotationsLayer";

            AddLayer(topoLayer);
            AddLayer(imageryLayer);
            AddLayer(streetLayer);

            AddLayer(g1);
            AddLayer(g2);
            AddLayer(g3);
        }

        public void AddLayer(Layer newLayer)
        {
            layers.Add(newLayer);

            if (MapLayerAdded != null)
                MapLayerAdded(newLayer);
        }

        #region Map Layers Management

        public Layer AddLayer(string id, string url)
        {
            ArcGISTiledMapServiceLayer newLayer = new ArcGISTiledMapServiceLayer();
            newLayer.Url = url;
            newLayer.Visible = true;
            newLayer.Opacity = 1;
            newLayer.ID = id;
            newLayer.Visible = true;

            layers.Add(newLayer);
            if (MapLayerAdded != null)
                MapLayerAdded(newLayer);

            return newLayer;
        }

        public bool RemoveLayer(string id)
        {
            foreach (Layer layer in layers)
            {
                if (layer.ID == id)
                {
                    layers.Remove(layer);

                    if (MapLayerDeleted != null)
                        MapLayerDeleted(layer);

                    return true;
                }
            }
            return false;
        }

        #endregion

        public Layer CloneLayer(Layer originalLayer)
        {
            switch (originalLayer.GetType().Name)
            {
                case "ArcGISTiledMapServiceLayer":
                    ArcGISTiledMapServiceLayer cloneLayer = new ArcGISTiledMapServiceLayer();
                    cloneLayer.Url = (originalLayer as ArcGISTiledMapServiceLayer).Url;
                    cloneLayer.ID = (originalLayer as ArcGISTiledMapServiceLayer).ID;
                    cloneLayer.Opacity = (originalLayer as ArcGISTiledMapServiceLayer).Opacity;
                    cloneLayer.Visible = (originalLayer as ArcGISTiledMapServiceLayer).Visible;
                    return cloneLayer;
                case "ArcGISDynamicMapServiceLayer":
                    ArcGISDynamicMapServiceLayer cloneLayer2 = new ArcGISDynamicMapServiceLayer();
                    cloneLayer2.Url = (originalLayer as ArcGISDynamicMapServiceLayer).Url;
                    cloneLayer2.ID = (originalLayer as ArcGISDynamicMapServiceLayer).ID;
                    cloneLayer2.Opacity = (originalLayer as ArcGISDynamicMapServiceLayer).Opacity;
                    cloneLayer2.Visible = (originalLayer as ArcGISDynamicMapServiceLayer).Visible;
                    return cloneLayer2;
                case "GraphicsLayer":
                    GraphicsLayer cloneLayer3 = new GraphicsLayer();
                    cloneLayer3.ID = (originalLayer as GraphicsLayer).ID;
                    cloneLayer3.Opacity = (originalLayer as GraphicsLayer).Opacity;
                    cloneLayer3.Visible = (originalLayer as GraphicsLayer).Visible;
                    return cloneLayer3;
                default:
                    return null;

            }
        }
    }

}